<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
    <title>菩提阁</title>
    <link rel="icon" href="./../images/favico.ico">
    <!--不同屏幕尺寸根字体设置-->
    <script src="./../js/base.js"></script>
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css" />
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css" />
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/address-edit.css" />
</head>
<body>
<div id="address_edit" class="app">
    <div class="divHead">
        <div class="divTitle">
            <i class="el-icon-arrow-left" @click="goBack"></i>{{title}}
        </div>
    </div>
    <div class="divContent">
        <div class="divItem">
            <span>头 像：</span>
            <el-upload
                    class="avatar-uploader"
                    action="/common/upload"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                    :on-change="onChange"
                    ref="upload"
            >
                <img
                        style="width: 50px;height: 50px;"
                        v-if="imageUrl"
                        :src="imageUrl"
                        class="avatar"
                ></img>
                <i
                        v-else
                        class="el-icon-plus avatar-uploader-icon"
                ></i>
            </el-upload>
        </div>
        <div class="divItem">
            <span>昵 称：</span>
            <el-input placeholder=" 请填写您的昵称" v-model="form.name"  maxlength='10' class="inputUser"/></el-input>
        </div>
        <div class="divItem">
            <span>手机号：</span>
            <el-input placeholder=" 请填写您的手机号码" v-model="form.phone"  maxlength='20' style="width: calc(100% - 80rem);"/></el-input>
        </div>
        <div class="divItem">
            <span>性 别：</span>
            <span class="spanChecked" @click="form.sex = '1'">
                    <i :class="{iActive:form.sex === '1'}"></i>
                    先生
                   </span>
            <span class="spanChecked" @click="form.sex = '0'">
                    <i :class="{iActive:form.sex === '0'}"></i>
                    女士
                </span>
        </div>
        <div class="divItem">
            <span>身份证号：</span>
            <el-input placeholder=" 请填写您的身份证号码" v-model="form.idNumber"  maxlength='20' style="width: calc(100% - 80rem);"/></el-input>
        </div>

        <div class="divSave" @click="editUser">修改信息</div>
    </div>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant样式 -->
<script src="./../js/vant.min.js"></script>
<script src="./../js/common.js"></script>
<script src="./../api/user.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script>
    new Vue({
        el:"#address_edit",
        data(){
            return {
                title:'个人信息',
                form:{
                    id:'',
                    name:'',//联系人
                    phone:undefined,//手机号
                    sex:'1',//0表示女 1 表示男
                    idNumber:'',//收货地址
                    avatar:'',//头像
                    status:0,
                },
                id:undefined,
                imageUrl:''
            }
        },
        computed:{},
        created(){
            this.initData()
        },
        mounted(){
        },
        methods:{
            goBack(){
                history.go(-1)
            },
            async initData(){
                this.form = JSON.parse(sessionStorage.getItem("user"))
                this.imageUrl = `/common/download?name=${this.form.avatar}`
            },
            async editUser(){
                const form = this.form
                const reg = /^1[3|4|5|7|8][0-9]{9}$/
                if(!reg.test(form.phone)){
                    this.$notify({ type:'warning', message:'手机号码不合法'});
                    return
                }
                const res = await editUserApi(form)
                if(res.code === 1){
                    window.requestAnimationFrame(()=>{
                        window.location.replace('/front/page/user.html')
                    })
                }else{
                    this.$notify({ type:'warning', message:res.msg});
                }
            },
            handleAvatarSuccess (response, file, fileList) {
                // 拼接down接口预览
                if(response.code === 0 && response.msg === '未登录'){
                    window.top.location.href = '/front/page/login.html'
                }else{
                    this.imageUrl = `/common/download?name=${response.data}`
                    this.form.avatar = response.data
                }
            },
            onChange (file) {
                if(file){
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            }
        }
    })
</script>
</body>
</html>
